let express = require("express")();
let expressStatic = require("express-static");
let mysql = require("mysql");

const port = 83;

let sql = mysql.createConnection({
    host      : 'localhost',
    user      : 'root',
    password  : '123456',
    database  : 'massage'
});

sql.connect();  

express.get("/getStudentList",(request,response)=>{
    sql.query(`select * from student`,(error,data)=>{
        if(error){
            response.send("error")
        }
        else{
            response.send(data)
        }
    })
})


express.get("/deleteStudent",(request,response)=>{
    console.log(request.query)
    let id = request.query.id;
    sql.query(`delete from student where id=${id}`,(error,data)=>{
        if(error){
            response.send("error")
        }
        else{
            response.send("success")
        }
    })
})

express.get("/addStudent",(request,response)=>{
   
    let p = request.query;
    console.log(p)
	sql.query(`insert into student (name,age,sex,joinDate,city) VALUES ("${p.name}","${p.age}","${p.sex}","${p.joinDate}","${p.city}")`,(error,data)=>{
		if(error){
			console.log(error)
			response.send("error")
		}
		else{
			response.send("success")
			console.log(`新增学生成功`)
		}
	})
})

express.use(expressStatic(__dirname + '/static'));

express.listen(port);
console.log(`express is running at ${port}`)


